<?php
	/* -------------------------------------------------------------------
	IOU - expense sharing system 
	Copyright (C) 2009 - Mischa Spelt (m.spelt@gmail.com)
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
	------------------------------------------------------------------- */

	include "lib/lib.main.php";
	checklogin();

	if(isset($_POST['add'])) {
		$from = $_POST['direction'] == 'in' ? $_POST['user'] : $gUser['ID'];
		$to = $_POST['direction'] == 'in' ? $gUser['ID'] : $_POST['user'];
		$_POST['amount'] = str_replace(',', '.', $_POST['amount']);
		$description = "Repayment by " . $db->q("value SELECT DisplayName FROM Users WHERE ID = %i", $from) . " to " . $db->q("value SELECT DisplayName FROM Users WHERE ID = %i", $to);
		$billID = $db->q("returnid INSERT INTO Bills(Date, Description, GroupID, CreatedBy, IsRepayment) VALUES(%s, %s, %i, %i, -1)", $_POST['date'], $description, $_POST['group'], $gUser['ID']);
		$db->q("INSERT INTO Journal(BillID, UserID, Description, Amount, Divisor, IsPayment) VALUES(%i, %i, %s, %f, 1, 0)", $billID, $from, $description, -$_POST['amount']);		
		$db->q("INSERT INTO Journal(BillID, UserID, Description, Amount, Divisor, IsPayment) VALUES(%i, %i, %s, %f, 1, 0)", $billID, $to, $description, $_POST['amount']);
		header("Location: bill.php?id=$billID");
	}

	print_header("Enter repayment");
	// Only allow repayments within one of my groups
	$myGroups = $db->q("table SELECT ID, DisplayName FROM Users_Groups LEFT JOIN Groups ON Users_Groups.GroupID = Groups.ID WHERE UserID = %i AND Users_Groups.IsActive = -1", $gUser['ID']);
	print "<script type='text/javascript'>" .
		"function AddSelectOption(selectObj, text, value, isSelected) { if (selectObj != null && selectObj.options != null) { selectObj.options[selectObj.options.length] = new Option(text, value, false, isSelected); } }" . // from http://stevenharman.net/blog/archive/2007/07/10/add-option-elements-to-a-select-list-with-javascript.aspx
		"function updateUserList(groupID) { " .
		"var select = document.getElementById('user');" .
		"while (select.options.length) select.options[0] = null; // clear the select \n " .		
		"if(groupID == 0) {" .
			"document.getElementById('user').value = 0;" .
			"document.getElementById('add').disabled = true;" .
			"AddSelectOption(select, \"-- No group --\", 0, false);" .
			"return;" .
		"}" .
		"AddSelectOption(select, \"-- Choose user --\", 0, false);";
	foreach($myGroups as $group) {
		print "if(groupID == {$group['ID']}) {";
		$users = $db->q("table SELECT ID, Login FROM Users_Groups LEFT JOIN Users ON Users_Groups.UserID = Users.ID WHERE GroupID = %i AND NOT (UserID = %i) AND Users_Groups.IsActive = -1", $group['ID'], $gUser['ID']);
		foreach($users as $user) print "AddSelectOption(select, \"{$user['Login']}\", {$user['ID']}, false);";
		print "}";
	}
	print "}";
	print "</script>";

	print "<h1>Enter a repayment</h1>";
	print "<form method='post'><table>";
	print "<tr><td><label for='group' accesskey='P'>Grou<span class='accesskey'>p</p></label></td><td><select name='group' id='group' onchange='updateUserList(this.value);'>";
	foreach($myGroups as $group) print "<option value='{$group['ID']}'" . ($group['ID'] == $gUser['PreferredGroup'] ? " selected='selected'" : "") . ">{$group['DisplayName']}</option>";
	print "</select></td></tr>";
	
	print "<tr><td>Payment</td><td>" .
		"<input type='radio' name='direction' value='in' id='dir:in' checked='checked'><label for='dir:in' accesskey='T'><span class='accesskey'>t</span>o me by</label>" .
		"<input type='radio' name='direction' value='out' id='dir:out'><label for='dir:out' accesskey='Y'>b<span class='accesskey'>y</span> me to</label>" .
		"</td></tr>";
	$users = $db->q("table SELECT ID, Login FROM Users_Groups LEFT JOIN Users ON Users_Groups.UserID = Users.ID WHERE GroupID = %i AND NOT (UserID = %i) AND Users_Groups.IsActive = -1", $gUser['PreferredGroup'], $gUser['ID']);
	print "<tr><td><label for='user' accesskey='O'><span class='accesskey'>o</span>ther person:</label></td><td><select name='user' id='user' onchange='document.getElementById(\"add\").disabled = (this.value == 0);'><option value='0'>-- Choose user--</option>";
	foreach($users as $user) 
		print "<option value='{$user['ID']}'>{$user['Login']}</option>";
	print "</select></td></tr>";

	print "<tr><td><label for='date' accesskey='D'><span class='accesskey'>D</span>ate</label></td><td><input type='text' size='10' name='date' id='date' value='" . date('Y-m-d') . "'> (yyyy-mm-dd)</td></tr>";
	print "<tr><td><label for='amount' accesskey='A'><span class='accesskey'>A</span>mount:</label></td><td><input type='text' name='amount' id='amount'></td></tr>";
	print "<tr><td colspan='2' style='text-align: center;'><input type='submit' id='add' name='add' accesskey='E' value='Enter repayment' disabled='disabled'></td></tr>";
	print "</table></form>";

	focusOn('group');

	print_footer();
?>
